<?php 
namespace Zbsht\Controller;



class RbacController extends CommentController{

	//用户列表
	public function index(){
		$this->role = D('UserRelation')->field('password',true)->relation(true)->select();


		$this->display('index');
	}



	//角色列表
	public function role(){
		$this->role = M('role')->select();


		$this->display('listRole');
	}


	//节点列表
	public function node(){
		$field = ['id','name','title','pid','status','level'];
		$node = M('node')->field($field)->order('sort desc')->select();
		$this->node = node_merge($node);

		// echo '<pre>';
		// print_r($node);

		$this->display('listNode');
	}


	//添加用户
	public function addUser(){

		if (IS_POST) {
			$post = I('post.');
			$user = [
				'username'	=>	$post['username'],
				'password'	=>	md5($post['password']),
				'create_ip'	=>	get_client_ip(),
				'create_time'=> time(),
			];
			$role = [];
			if ($uid = M('user')->add($user)) {
				foreach ($post['role_id'] as $v) {
					$role[] = [
						'role_id'	=> 	$v,
						'user_id'	=>	$uid,
					];
				}
				M('role_user')->addAll($role);
				$this->success('添加成功',U('index'));
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->role = M('role')->where(['status'=>1])->select();
			$this->display();
		}

	}


	//添加角色
	public function addRole(){



		$this->display();
	}


	//添加角色表单处理
	public function addRoleHandle(){
		$post = I('post.');
		if (M('role')->add($post)) {
			$this->success('添加成功',U('role'));
		}else{
			$this->error('添加失败');
		}
	}


	//添加节点
	public function addNode(){

		$this->pid = I('pid' , 0 , 'intval');
		$this->level = I('level' , 1 , 'intval');

		switch ($this->level) {
			case '1':
				$this->type = '应用';
				break;
			case '2':
				$this->type = '控制器';
				break;
			case '3':
				$this->type = '动作方法';
				break;
		}


		$this->display('addNode');
	}


	//添加节点处理
	public function addNodeHandle(){

		if (M('node')->add(I('post.'))) {
			$this->success('添加成功',U('node'));
		}else{
			$this->error('添加失败');
		}
	}


	// 配置权限
	public function access(){
		$rid = I('rid' , 0 , 'intval');

		$field = ['id','name','title','pid','level'];
		// 所有节点
		$node = M('node')->field($field)->order('sort')->select();
		// 原有权项 
		$access = M('access')->where(['role_id'=>$rid])->getField('node_id',true);

		$this->node = node_merge($node , $access);

		// echo '<pre>';
		// print_r($this->node);
		$this->rid = $rid;
		$this->display();
	}


	// 修改权项
	public function setAccess(){
		$post = I('post.' , 0);
		$db = M('access');

		// 清空新权项
		$db->where(['role_id'=>$post['rid']])->delete();

		// 给合新权项
		$data = [];
		foreach ($post['access'] as $v) {
			$tmp = explode('_' , $v);
			$data[] = [
				'role_id'	=> 	$post['rid'],
				'node_id'	=>	$tmp[0],
				'level'		=> 	$tmp[1],
			];
		}

		$db->startTrans();  // 开启一个事物
		try {
			$info = $db->addAll($data);
			$db->commit();  // 执行成功， 提交事物

			$this->success('修改成功！',U('role'));
		} catch (Exception $e) {
			$db->rollback(); // 报错事物数据  回滚 
			$this->error('修改失败！');
		}
	}




}





 ?>